<%@page import="com.dao.CateDao"%>
<%@page import="com.beans.Cateinfo"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
    List<Cateinfo> cateLists=new CateDao().getAllCates(); 
	request.setAttribute("cateLists",cateLists);%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>"> 
<meta charset="UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">  
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="css/edittable.css"  ></link>  
<link rel="stylesheet" type="text/css" href="css/validate.css"  ></link>  
<script type="text/javascript"  src="js/jquery-1.8.0.js"></script>
<script type="text/javascript"  src="js/goods_edit.js"></script>
<script type="text/javascript">
//获取图片大小
function getFileSize(eleId) {
    try {
        var size = 0;
        size = $('#' + eleId)[0].files[0].size;//byte
        size = size / 1024;//kb
        size = size / 1024;//mb
        //alert('上传文件大小为' + size + 'M');
        return size;
    } catch (e) {
        alert("错误：" + e);
        return -1;
    }
}

$(function(){   
	$("#select1").change(function(){
		$("#select2").empty();
		var pId=this.value;
		if(pId=="-1"){
			$("#select2").append("<option value='-1'>请选择</option>");
		}
		<c:forEach var="m" items="${cateLists }">
			if('${m.id}'==$('#select1 option:selected').val()){
				<c:forEach var="n" items="${m.cateinfos }">
				var str="<option value='${n.id}'>${n.cateName}</option>" ;
				$("#select2").append(str);
				</c:forEach>
			};
		</c:forEach>			
	});
	$("#pictureData").change(function(){
		var name = this.value;
	    var fileName = name.substring(name.lastIndexOf(".")+1).toLowerCase();
	    if(fileName !="jpg" && fileName !="jpeg" ){
	    	showError("图片格式必须为.jpg", this);
	    }else if (getFileSize("pictureData")>5) {
	    	showError("图片不应大于5M", this);
		}else {
			showOk("校核完成", this);
		}
	});
});
</script>
</head>
<body>
<div class ="div_title">
	 <div class="div_titlename"> <img src="images/san_jiao.gif" ><span>商品添加</span></div>
</div>

<form action="GoodsServlet.do?flag=add" method="post" onsubmit="return validate();" name="form1" enctype="multipart/form-data"  >
	<table class="edit_table" >
	
	<tr>
	 	<td class="td_info">商品名称:</td>	
	 	<td class="td_input_short"> 
	 		<input type="text" class="txtbox" id="goodsName" name="goodsName" /> 
	 	</td>   
	 	<td>
	 		<label class="validate_info" id="goodsName_msg">2-20位非空白字符</label>
	 	</td> 
	</tr>
	
	<tr>
	 	<td class="td_info">所属分类:</td>	
	 	<td class="td_input_short"> 
	 		<select id="select1" name="bigCateId">
			   <option value="-1">请选择</option>
				<c:forEach var="m" items="${cateLists }">
					<option value="${ m.id}">${ m.cateName}</option>
				</c:forEach>
			</select>
			<select id="select2" name="smallCateId">
			   <option value="-1">请选择</option>
			</select>
						
	 	</td>   
	 	<td>
	 		<label class="validate_info" id="select1_msg">请指定商品所属分类</label>
	 	</td> 
	</tr>
	
	<tr>
	 	<td class="td_info">计量单位:</td>	
	 	<td class="td_input_short"> 
	 		<input type="text" class="txtbox" id="unit" name="unit" /> 
	 	</td>   
	 	<td>
	 		<label class="validate_info" id="unit_msg">1-10位非空白字符</label>
	 	</td> 
	</tr>
	
	<tr>
	 	<td class="td_info">商品价格:</td>	
	 	<td class="td_input_short"> 
	 		<input type="text" class="txtbox" id="price" name="price" /> 
	 	</td>   
	 	<td>
	 		<label class="validate_info" id="price_msg">不能为空，以元为单位，可以是小数</label>
	 	</td> 
	</tr>
	
	<tr>
	 	<td class="td_info">生产厂商:</td>	
	 	<td class="td_input_short"> 
	 		<input type="text" class="txtbox" id="producter" name="producter" /> 
	 	</td>   
	 	<td>
	 		<label class="validate_info" id="producter_msg"></label>
	 	</td> 
	</tr>
	
	<tr>
	 	<td class="td_info">商品图片:</td>	
	 	<td class="td_input_short"> 
	 		<input type="file" id="pictureData" name="pictureData" class="uploadPic" onchange="handleUpload(this)">
        	<img src="" id="show" class="showImg">
	 	</td>   
	 	<td>
	 		<label class="validate_info" id="pictureData_msg">必须为大小在5M以内的.jpg图片</label>
	 	</td> 
	</tr>
	
	<tr>
		<td class="td_info">分类描述:</td>	
		<td><textarea rows="4" cols="27" name="des" class="txtarea" ></textarea> </td>	
		<td><label></label></td>	
	</tr>
	
	<tr>
		<td class="td_info"> </td>	
		<td> 
		<input class="form_btn" type="submit" value="提交" /> 
		<input type="reset"  class="form_btn" value="重置" /> </td>	
		<td>
			<label id="result_msg" class="result_msg">${msg}</label>
		</td>	
	</tr>
	
	</table>	 	 	
</form>
<script type="text/javascript">
function handleUpload(obj) {
    if (obj.value === '') {
        return // 这里防止第二次选择图片时候点击取消出现报错
    }
    console.log(obj) // 输出了id="file"的dom对象
    // var inputObj = document.getElementById('file') // dom获取
    var imgObject = obj.files[0]
    console.log(imgObject) // 输出了图片的信息
    // console.log(typeof imgObject) // 图片是个对象类型
    var showPicURL = getObjectURL(imgObject)

    // 设置显示预览
    var show = document.getElementById('show')
    show.src = showPicURL
}

// 获取图片URL的兼容写法
function getObjectURL(file) {
    var url = null
    if (window.createObjectURL != undefined) { // 基础判断
        url = window.createObjectURL(file)

    } else if (window.URL != undefined) { // 判断 mozilla (firefox)
        url = window.URL.createObjectURL(file)

    } else if (window.webkitURL != undefined) { // 判断 webkit 和 chrome 浏览器
        url = window.webkitURL.createObjectURL(file)
    }

    return url // 返回获得的url
}
</script>
</body>
</html>